<template>
  <div class="goods-hot">
    <!-- obj[变量名] -->
    <h3>{{ titleObj[type] }}</h3>
    <!-- 商品区块 -->
    <goods-item
      style="width: 100%"
      v-for="good in hotData"
      :key="good.id"
      :good="good"
    />
  </div>
</template>
<script>
// 复用分类的商品子组件
import GoodsItem from '@/views/Category/components/goods-item'
import { ref, onMounted } from 'vue'
import { findHotGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
const useHotData = (props) => {
  const hotData = ref([])
  const route = useRoute()
  const getHotData = async () => {
    const { result } = await findHotGoods({ id: route.params.id, type: props.type })
    console.log('hot:', result)
    hotData.value = result
  }
  onMounted(() => {
    getHotData()
  })
  return { hotData }
}
export default {
  name: 'GoodsHot',
  components: {
    GoodsItem
  },
  props: {
    type: {
      type: Number,
      default: 1
    }
  },
  setup (props) {
    // 复用的榜单title
    // 1. 普通的js对象=> 不是一个响应数据=》注意：修改数据后，模板不会刷新
    const titleObj = { 1: '24小时热销榜', 2: '周热销榜', 3: '总热销榜' }
    // 2. 通过ref定义的响应数据
    // const titleObj = ref({ 1: '24小时热销榜', 2: '周热销榜', 3: '总热销榜' })
    // 根据type获取热榜数据
    const { hotData } = useHotData(props)
    return { titleObj, hotData }
  }
}
</script>
<style scoped lang="less">
.goods-hot {
  h3 {
    height: 70px;
    background: @helpColor;
    color: #fff;
    font-size: 18px;
    line-height: 70px;
    padding-left: 25px;
    margin-bottom: 10px;
    font-weight: normal;
  }
}
</style>
